package com.yy.jhl.ui.user.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.FormatUtil;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.interfaces.MessageNumCallBack;
import com.yy.jhl.ui.user.contract.FindPwdContract;
import com.yy.jhl.ui.user.presenter.FindPwdPresenter;
import com.yy.jhl.util.DataRequestUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/6.
 */

public class FindPwdActivity extends BaseActivity<FindPwdPresenter> implements FindPwdContract.View {
    @BindView(R.id.phone_edit)
    EditText phoneEdit;
    @BindView(R.id.code_edit)
    EditText codeEdit;
    @BindView(R.id.send_code_btn)
    Button sendCodeBtn;
    @BindView(R.id.first_step_view)
    LinearLayout firstStepView;
    @BindView(R.id.pwd_edit)
    EditText pwdEdit;
    @BindView(R.id.new_ensure_edit)
    EditText newEnsureEdit;
    @BindView(R.id.second_step_view)
    LinearLayout secondStepView;
    @BindView(R.id.ensure_btn)
    Button ensureBtn;
    private int loginType = 1;//1:供应商 2：采购商
    private int currentStep = 1;//1:第一步 2：第二步
    public static void startAction(Context mContext, int loginType) {
        Intent intent = new Intent(mContext, FindPwdActivity.class);
        intent.putExtra("loginType", loginType);
        mContext.startActivity(intent);
    }
    @Override
    public int getLayoutId() {
        return R.layout.act_find_pwd;
    }

    @Override
    public void initView() {
        loginType = getIntent().getIntExtra("loginType",1);
        showTitle("找回密码", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        sendCodeBtn.setClickable(false);
        phoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String phone = charSequence.toString();
                if (!StringUtil.isEmpty(phone)){
                    sendCodeBtn.setClickable(true);
                    sendCodeBtn.setBackgroundResource(R.drawable.green_btn_bg);
                }else {
                    sendCodeBtn.setClickable(false);
                    sendCodeBtn.setBackgroundResource(R.drawable.gray_btn_bg);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        //为了获取新的token
        DataRequestUtil.getInstance(mContext).getUnReadMessageNum(new MessageNumCallBack() {
            @Override
            public void getCallBack(int total) {

            }
        });
    }

    @OnClick({R.id.send_code_btn, R.id.ensure_btn})
    public void onViewClicked(View view) {
        String phone = phoneEdit.getText().toString().trim();
        String code = codeEdit.getText().toString().trim();
        String pwd = pwdEdit.getText().toString().trim();
        String new_pwd = newEnsureEdit.getText().toString().trim();
        switch (view.getId()) {
            case R.id.send_code_btn:
                if (StringUtil.isEmpty(phone)){
                    showShortToast("手机号不能为空");
                    return;
                }
                if (!FormatUtil.isMobileNO(phone)){
                    showShortToast("手机号格式不正确");
                    return;
                }
                // TODO: 2017/12/9
                mPresenter.findPwd("1",phone,"","","");
                break;
            case R.id.ensure_btn:
                if (firstStepView.getVisibility() == View.VISIBLE){
                    if (StringUtil.isEmpty(phone)){
                        showShortToast("手机号不能为空");
                        return;
                    }
                    if (!FormatUtil.isMobileNO(phone)){
                        showShortToast("手机号格式不正确");
                        return;
                    }
                    if (StringUtil.isEmpty(code)){
                        showShortToast("验证码不能为空");
                        return;
                    }
                    firstStepView.setVisibility(View.GONE);
                    secondStepView.setVisibility(View.VISIBLE);
                    currentStep = 2;
                }else {
                    if (StringUtil.isEmpty(pwd)){
                        showShortToast("新密码不能为空");
                        return;
                    }
                    if (pwd.length() < 6 || pwd.length() > 30){
                        showShortToast("请输入6-30位的新密码");
                        return;
                    }
                    if (StringUtil.isEmpty(new_pwd)){
                        showShortToast("确认密码不能为空");
                        return;
                    }
                    if (pwd.length() < 6 || pwd.length() > 30){
                        showShortToast("请输入6-30位的确认密码");
                        return;
                    }
                    if (!pwd.equals(new_pwd)){
                        showShortToast("密码输入不一致");
                        return;
                    }
                    // TODO: 2017/12/9
                    mPresenter.findPwd("2",phone,code,pwd,new_pwd);

                }
                break;
        }
    }

    @Override
    public void findSuccess() {
        if (currentStep == 1){
            showShortToast("验证码发送成功");
            sendCodeBtn.setClickable(false);
            sendCodeBtn.setBackgroundResource(R.drawable.gray_btn_bg);
            min = 60;
            timeRunnable.run();
        }else {
            showShortToast("找回密码成功");
            finish();
        }
    }

    @Override
    public void findFail() {

    }
    Handler handler = new Handler(){};
    int min = 60;
    /** 倒计时*/
    Runnable timeRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                if(min > 0){
                    min--;
                    sendCodeBtn.setClickable(false);
                    sendCodeBtn.setBackgroundResource(R.drawable.gray_btn_bg);
                    sendCodeBtn.setText(min + "s");
                    handler.postDelayed(timeRunnable, 1000);
                }else{
                    sendCodeBtn.setText("获取验证码");
                    sendCodeBtn.setClickable(true);
                    sendCodeBtn.setBackgroundResource(R.drawable.green_btn_bg);
                }
            }catch (Exception e){
            }
        }
    };
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                &&event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0){
            if (firstStepView.getVisibility() == View.GONE){
                currentStep = 1;
                firstStepView.setVisibility(View.VISIBLE);
                secondStepView.setVisibility(View.GONE);
                return false;
            }
        }else{
        }
        return super.dispatchKeyEvent(event);
    }
}
